﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IntroduceCompany.Models;
using IntroduceCompany.Singleton;
using IntroduceCompany.Util;

namespace IntroduceCompany.Controllers
{
    [HandleError]
    [CheckModule(ConstantClass.Backend_Menu_MenuID)]
    public class MenuListController : BaseController
    {

        #region BackEndMenuList


        [Authorize(Roles = ConstantClass.autMenu)]
        public ActionResult ViewAllMenuList()
        {
            //Check Cookie Language
            string languageID = ConstantClass.markViewAllLanguage;
            if (Request.Cookies[ConstantClass.cooLanguage] != null && Request.Cookies[ConstantClass.cooLanguage].Value != "")
            {
                languageID = Request.Cookies[ConstantClass.cooLanguage].Value;
            }

            //Check Cookie Language
            string menuTypeID = "";
            if (Request.Cookies[ConstantClass.cooMenuType] != null && Request.Cookies[ConstantClass.cooMenuType].Value != "")
            {
                menuTypeID = Request.Cookies[ConstantClass.cooMenuType].Value;
            }

            if (Session["MenuListEdit"] == null)
            {
                //Create a MenuListView object for ViewData in partial MenuListEdit
                Session["MenuListEdit"] = new IntroduceCompany.MenuListView();
            }
            
            //ViewData for MenuList
            ViewData["MenuTypeList"] = new SelectList(DataSingleton.GetMenuListModels().GetMenuType().ToList(), "MenuTypeID", "MenuTypeName", menuTypeID);
            ViewData["LaguageList"] = new SelectList(DataSingleton.GetLanguageModels().GetLanguageList().ToList(), "LanguageID", "LanguageName", languageID);

            //ViewData For MenuListEdit
            ViewData["MenuTypeListEdit"] = new SelectList(DataSingleton.GetMenuListModels().GetMenuType().ToList(), "MenuTypeID", "MenuTypeName");
            ViewData["MenuListEditLanguage"] = new SelectList(DataSingleton.GetLanguageModels().GetLanguageList().ToList(), "LanguageID", "LanguageName");
            ViewData["DDLMenuList"] = new SelectList(DataSingleton.GetMenuListModels().GetMenuList(languageID,1), "MenuListID", "MenuName");

            return View("/Views/Backend/Admin/ViewAllMenuList.aspx", DataSingleton.GetMenuListModels().GetMenuList(languageID, menuTypeID));
        }


        //[Authorize(Roles = ConstantClass.autMenu)]
        //[HttpPost, ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        //public ActionResult ViewAllMenuList(String languageID)
        //{
        //    ViewData["LaguageList"] = new SelectList(DataSingleton.GetLanguageModels().GetLanguageList().ToList(), "LanguageID", "LanguageName");
        //    return View("/Views/Backend/Admin/ViewAllMenuList", DataSingleton.GetMenuListModels().GetMenuList(languageID));
        //}


        [Authorize(Roles = ConstantClass.autMenu)]
        [HttpPost, ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult ChangeMenuType(String menuTypeID)
        {
            if (Request.Cookies[ConstantClass.cooMenuType] == null)
            {
                HttpCookie cookie = new HttpCookie(ConstantClass.cooMenuType, menuTypeID);
                Response.Cookies.Add(cookie);
                cookie.Expires = DateTime.Now.AddDays(ConstantClass.cooMenuTypeTimeLife);
            }
            else
            {
                Response.Cookies[ConstantClass.cooMenuType].Value = menuTypeID;
            }

            return RedirectToAction("ViewAllMenuList", "MenuList");
        }


        /// <summary>
        /// Change Menu Status that is visible or invisible
        /// </summary>
        /// <param name="menuListID"></para
        /// <param name="menuVisible"></param>
        /// <returns></returns>
        [Authorize(Roles = ConstantClass.autMenu)]
        public ActionResult ChangeMenuVisible(int menuListID, bool menuVisible)
        {
            DataSingleton.GetMenuListModels().ChangeMenuVisible(menuListID, menuVisible);
            return RedirectToAction("ViewAllMenuList", "MenuList");
        }


        /// <summary>
        /// Delete a Menu
        /// </summary>
        /// <param name="menuListID"></param>
        /// <returns></returns>
        [Authorize(Roles = ConstantClass.autMenu)]
        public ActionResult MenuListDelete(int menuListID)
        {
            DataSingleton.GetMenuListModels().DeleteMenuList(menuListID, (Request.PhysicalApplicationPath.ToString() + "Content/Backend/images/"));
            return RedirectToAction("ViewAllMenuList", "MenuList");
        }

        [Authorize(Roles = ConstantClass.autMenu)]
        public ActionResult MenuListEdit(int menuListID, String menuLanguageID)
        {

            //Get information of a menu and pass to partial MenuListEdit
            MenuListView menuListView = DataSingleton.GetMenuListModels().GetMenuByID(menuListID, menuLanguageID);
            if (menuListView == null)
            {
                Session["MenuListEdit"] = new IntroduceCompany.MenuListView();
            }
            else
            {
                Session["MenuListEdit"] = DataSingleton.GetMenuListModels().GetMenuByID(menuListID, menuLanguageID);
            }

            return RedirectToAction("ViewAllMenuList", "MenuList");
        }


        [Authorize(Roles = ConstantClass.autMenu)]
        [HttpPost, ValidateInput(false), MultiButton(MatchFormKey = "action", MatchFormValue = "Lưu Menu")]
        [ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult MenuListEdit(MenuListView menuListView, HttpPostedFileBase uploadFile, String menuImage)
        {
            string languageID = "1";
            if (Request.Cookies[ConstantClass.cooLanguage] != null && Request.Cookies[ConstantClass.cooLanguage].Value != "")
            {
                languageID = Request.Cookies[ConstantClass.cooLanguage].Value;
            }

            int menuListMaxID = 0;


            string newImageMenu = "";
            if (uploadFile != null && uploadFile.ContentLength > 0)
            {
                //Upload Image of Menu
                newImageMenu = DataSingleton.GetGeneralModels().MenuListUploadImage(uploadFile, Request.PhysicalApplicationPath.ToString(), menuImage);
            }


            //Create MenuList
            if (menuListView.MenuListID == 0)
            {
                if (ModelState.IsValid)
                {
                    if (TryUpdateModel(menuListView))
                    {
                        menuListMaxID = DataSingleton.GetMenuListModels().MenuListCreate_1(menuListView, newImageMenu);
                    }
                }
            }
            else if (menuListView.MenuListID != 0 && !DataSingleton.GetMenuListModels().CheckMenuListLanguage(menuListView)) //Add other language menu into MenuList_L
            {
                if (ModelState.IsValid)
                {
                    if (TryUpdateModel(menuListView))
                    {
                        DataSingleton.GetMenuListModels().MenuListCreate_2(menuListView.MenuListID, menuListView.LanguageID, menuListView.MenuName);
                    }
                }
            }
            else if (menuListView.MenuListID != 0) //Edit MenuList
            {

                if (ModelState.IsValid)
                {
                    if (TryUpdateModel(menuListView))
                    {
                        DataSingleton.GetMenuListModels().EditMenuList(menuListView, newImageMenu);
                    }
                }
                //ModelState.AddModelError("NoticePage", "Đã cập nhật thành công.");
            }


            if (menuListMaxID == 0)
            {
                //Get information of a menu and pass to partial MenuListEdit
                Session["MenuListEdit"] = menuListView;
            }
            else
            {
                //Get information of a new inserted menu and pass to partial MenuListEdit
                Session["MenuListEdit"] = DataSingleton.GetMenuListModels().GetMenuByID(menuListMaxID, menuListView.LanguageID);
            }

            return RedirectToAction("ViewAllMenuList", "MenuList");

        }




        [Authorize(Roles = ConstantClass.autMenu)]
        [HttpPost, ValidateInput(false), MultiButton(MatchFormKey = "action", MatchFormValue = "Tạo Mới")]
        [ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult MenuListCreate()
        {
            Session.Remove("MenuListEdit");
            return RedirectToAction("ViewAllMenuList", "MenuList");
        }


        /// <summary>
        /// Load MenuList for Backend master page
        /// </summary>
        /// <returns></returns>
        [ChildActionOnly]
        [Authorize]
        public ActionResult LeftMenu()
        {
            ViewData["AdminMenuList"] = DataSingleton.GetMenuListModels().GetBackEndMenuList();
            return View("/Views/Shared/Backend/LeftMenu.ascx");
        }

        #endregion



        #region FrontEnd_MenuList

        [ChildActionOnly]
        public ActionResult LoadHeaderMenu()
        {
            string languageID = ControllerContext.RequestContext.RouteData.Values["culture"].ToString();
            ViewData["FrontEndMenuList"] = DataSingleton.GetMenuListModels().GetAllFrontEndMenuListHtmlLink(languageID, 0);

            string current_Url = ControllerContext.HttpContext.Request.RawUrl;
            ViewData["CultureChooserUserControl"] = DataSingleton.GetLanguageModels().BuildCultureChooserUserControl(current_Url);
       
            return View("/Views/Shared/Frontend/HeaderMenu.ascx");
        }

        [ChildActionOnly]
        public ActionResult BuildSiteMapPath()
        {
            string current_Url = ControllerContext.HttpContext.Request.RawUrl;
            string languageID = ControllerContext.RequestContext.RouteData.Values["culture"].ToString();
            ViewData["SiteMapPathUserControl"] = DataSingleton.GetMenuListModels().BuildSiteMapPath(current_Url, languageID);
            return View("/Views/Shared/Frontend/SiteMapPathControl.ascx");
        }

        #endregion

    }
}
